package ocean.chinacraft.common.util;

import com.mojang.logging.LogUtils;
import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ArmorMaterial;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.AbstractCookingRecipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.phys.AABB;
import net.minecraftforge.common.ForgeHooks;
import org.slf4j.Logger;

import java.util.List;
import java.util.Random;

/**
 * Mod工具类
 *
 * @author zhang
 * @since 2021/11/17
 */
public class ModUtil {

    private static final Logger LOGGER = LogUtils.getLogger();

    /**
     * 是否是燃料、可燃烧
     *
     * @param itemStack   物品堆
     * @param iRecipeType 配方类型
     * @return 结果
     */
    public static boolean isFuel(ItemStack itemStack, RecipeType<? extends AbstractCookingRecipe> iRecipeType) {
        return ForgeHooks.getBurnTime(itemStack, iRecipeType) > 0 || itemStack.getItem() == Items.LAVA_BUCKET;
    }

    /**
     * 是否是燃料、可燃烧
     *
     * @param itemStack 物品堆
     * @return 结果
     */
    public static boolean isFuel(ItemStack itemStack) {
        return ForgeHooks.getBurnTime(itemStack, RecipeType.SMELTING) > 0 || itemStack.getItem() == Items.LAVA_BUCKET;
    }

    /**
     * 获取燃烧时间
     *
     * @param itemStack   物品堆
     * @param iRecipeType 配方类型
     * @return 结果
     */
    public static int getBurnTime(ItemStack itemStack, RecipeType<? extends AbstractCookingRecipe> iRecipeType) {
        return ForgeHooks.getBurnTime(itemStack, iRecipeType);
    }

    /**
     * 获取燃烧时间
     *
     * @param itemStack 物品堆
     * @return 结果
     */
    public static int getBurnTime(ItemStack itemStack) {
        return ForgeHooks.getBurnTime(itemStack, RecipeType.SMELTING);
    }

    /**
     * 生成1-100的随机数
     *
     * @return 结果
     */
    public static Integer random() {
        return (int) (Math.random() * (100 - 1) + 1);
    }

    public static float randomFloat(float max) {
        Random random = new Random();
        return random.nextFloat(max);
    }

    /**
     * 生成指定随机数
     *
     * @return 结果
     */
    public static Integer random(int max, int min) {
        return new Random().nextInt(max - min + 1) + min;
    }

    /**
     * Object数组转int数组
     *
     * @param target 目标字符
     * @return 结果
     */
    public static Integer[] getIntArray(Object[] target) {
        Integer[] ret = new Integer[target.length];
        for (int i = 0; i < target.length; i++) {
            ret[i] = Integer.parseInt(String.valueOf(target[i]));
        }
        return ret;
    }

    public static boolean isNotNull(ItemStack... items) {
        for (ItemStack item : items) {
            if (item == null || !(item.getItem() instanceof ArmorItem) || item.isEmpty()) {
                return false;
            }
        }
        return true;
    }

    public static List<Mob> getNearbyMobs(Player player, int range, TargetingConditions conditions) {
        AABB inflate = player.getBoundingBox().inflate(range, range, range);
        return player.level().getNearbyEntities(Mob.class, conditions, player, inflate);
    }

    public static List<Player> getNearbyPlayer(Player player, int range, TargetingConditions conditions) {
        AABB inflate = player.getBoundingBox().inflate(range, range, range);
        return player.level().getNearbyEntities(Player.class, conditions, player, inflate);
    }

    public static boolean hasCorrectArmorOn(ArmorMaterial material, Player player) {
        try {
            NonNullList<ItemStack> armorList = player.inventoryMenu.getItems();
            ItemStack boot = armorList.get(8);
            ItemStack leggings = armorList.get(7);
            ItemStack chest = armorList.get(6);
            ItemStack helmet = armorList.get(5);
            if (ModUtil.isNotNull(boot, leggings, chest, helmet)) {
                ArmorItem b = (ArmorItem) boot.getItem();
                ArmorItem l = (ArmorItem) leggings.getItem();
                ArmorItem c = (ArmorItem) chest.getItem();
                ArmorItem h = (ArmorItem) helmet.getItem();
                return h.getMaterial() == material && c.getMaterial() == material &&
                        l.getMaterial() == material && b.getMaterial() == material;
            }
        } catch (Exception e) {
            LOGGER.debug("似乎不是一个Armor，跳过此错误...");
        }
        return false;
    }
}
